package com.lovly.api;

import java.util.ArrayList;
import java.util.List;


public class PointValid {
	
	/**
	 * 
	 * @param cPoint
	 * @return
	 */
	public static Boolean valid(String cPoint,List<Point2D> point2ds,int codeLength){
		

		String[] arr = cPoint.split(",");

		List<Integer> list = new ArrayList<Integer>();

		for (int i = 0; i < arr.length; i++) {
			list.add(Integer.valueOf(arr[i].trim()));
		}
		
		Boolean flag = false;
		for (int i = 0; i < codeLength; i++) {
			Integer X1 = list.get(i * 2);
			Integer Y1 = list.get(i * 2 + 1);
			Point2D p = point2ds.get(i);
			flag = distencePC(X1, Y1, p.getX(), p.getY(), p.getR());
			if(!flag){
				break;
			}
		}
		return flag;
	}

	
	/**
	 * 位置验证方法
	 * 
	 * @param x1 点击x位置
	 * @param y1 点击Y位置
	 * @param x 初始位置x
	 * @param y 初始位置y
	 * @param r 半径
	 * @return
	 */
	private static Boolean distencePC(double x1, double y1, double x, double y, double r) {// 判断点与圆心之间的距离和圆半径的关系
		double d2 = Math.hypot((x - x1), (y - y1));

		if (d2 > r) {
			return false;
		} else {
			return true;
		}
	}
}
